package com.sdsy.controller;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpStatus;
import com.sdsy.constant.ResultErrorInfo;
import com.sdsy.domain.EthLoginDO;
import com.sdsy.entity.ResultEntity;
import com.sdsy.service.EthLoginService;
import com.sdsy.util.CommTypeUtils;
import com.sdsy.util.SysUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * (EthLogin)表控制层
 *
 * @author yxc
 * @since 2018-10-24 17:08:47
 */
@RestController
@RequestMapping("/ethLogin")
public class EthLoginController {
    /**
     * 服务对象
     */
    @Resource
    private EthLoginService ethLoginService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/login")
    @ResponseBody
    public ResultEntity login(EthLoginDO vo, HttpServletRequest request) {
        ResultEntity resultEntity = new ResultEntity();
        vo.setPassword(Md5PassWord(vo.getPassword()));
        EthLoginDO entity = ethLoginService.queryByUserNamePassWord(vo);
        if (SysUtil.isNull(entity)) {
            resultEntity.setCode(ResultErrorInfo.USER_NAME_PASSWORD_FAIL.getCode());
            resultEntity.setMessage(ResultErrorInfo.USER_NAME_PASSWORD_FAIL.getMsg());
        } else {
            HttpSession session = request.getSession();
            session.setAttribute(CommTypeUtils.LOGIN_MEMBER, entity);
            session.setMaxInactiveInterval(-1);
            resultEntity.setCode(HttpStatus.HTTP_OK);
            resultEntity.setMessage("成功");
        }
        return resultEntity;
    }

    public static void main(String[] args) {
        String password = "adminTcc131419";
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHex = md5.digestHex(password);
        System.out.println(digestHex);
    }

    public String Md5PassWord(String password) {
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHex = md5.digestHex(password);
        return digestHex;
    }

}